'use strict'

const { DataTypes, Model, Op } = require('../../../azeroth').sequelize;
class Job_2 extends Model { }

const initJobData_2 = (sequelize_aim) => {
    Job_2.init(
        {
            uid: {
                autoIncrement: true,
                type: DataTypes.INTEGER,
                allowNull: false,
                primaryKey: true
            },
            job_name: {
                type: DataTypes.STRING(128),
                allowNull: true
            },
            lot_number: {
                type: DataTypes.STRING(128),
                allowNull: true
            },
            sheet_sum: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            cut_mode: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            model_name: {
                type: DataTypes.STRING(128),
                allowNull: true
            },
            length: {
                type: DataTypes.DOUBLE(64, 2),
                allowNull: true
            },
            start_time: {
                type: DataTypes.BIGINT,
                allowNull: true
            },
            start_time_str: {
                type: DataTypes.STRING(64),
                allowNull: true
            },
            finish_time: {
                type: DataTypes.BIGINT,
                allowNull: true
            },
            finish_time_str: {
                type: DataTypes.STRING(64),
                allowNull: true
            },
            ok_number: {
                type: DataTypes.INTEGER.UNSIGNED,
                allowNull: true
            },
            ng_number: {
                type: DataTypes.INTEGER.UNSIGNED,
                allowNull: true
            }
        },
        {
            freezeTableName: true,
            sequelize: sequelize_aim,
            modelName: 'job',
            timestamps: false
        }
    )
}

Job_2.getMaxUid = async (sequelize_aim) => {
    let res = await sequelize_aim.query(`select max(uid) as uid from job`, { type: sequelize_aim.QueryTypes.SELECT });
    if (res.length) {
        return res[0].uid;
    } else {
        return 0;
    }
}

Job_2.upload = async (data) => {
    let res = await Job_2.bulkCreate([data], {
        updateOnDuplicate: ['uid', 'finish_time', 'finish_time_str', 'ok_number', 'ng_number']
    });
    return res;
}

module.exports = {
    Job_2,
    initJobData_2
};